package com.shengwei.truck.driver.bean;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
/**
 * 本地User类，用于保存用户信息
 */
public class User {
    private static String mUsername;
    private static Context mContext;
    public  static String mtoken;
    public  static String mtokenHead;
    public  static  User mUserInstance;
    public  static  String mHeadImvUrl;

    /**
     * 获取用户单例
     * @param context
     * @return
     */
     public static synchronized User getInstance(Context context) {
        if(mUserInstance == null) {
            mContext = context;
            mUserInstance = new User(context);
        }
        return mUserInstance;
    }

    /**
     * 构造函数，同时读取本地存储
     * @param context
     */
    public User(Context context) {
        this.mContext = context;
        SharedPreferences sp = mContext.getSharedPreferences("login",Context.MODE_PRIVATE);
        this.mUsername = sp.getString("username","");
        this.mtoken = sp.getString("token","");
        this.mtokenHead = sp.getString("tokenHead","");
        this.mHeadImvUrl = sp.getString("imvUrl","");
    }
    /**
     * 保存用户信息
     * @param user：登录接口返回的用户信息
     */
    public void saveUserInfo(ContentValues user)
    {
        if(user != null) {

            SharedPreferences loginPreference = mContext.getSharedPreferences("login", Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = loginPreference.edit();
            String username = user.getAsString("username");
            mtoken = user.getAsString("token");
            mtokenHead = user.getAsString("tokenHead");
            String headPicFilePath =  user.getAsString("headPicPath");

            if(!TextUtils.isEmpty(username)) {
                edit.putString("username", username);
            }
            if(!TextUtils.isEmpty(mtoken)) {
                edit.putString("token", mtoken);
            }
            if(!TextUtils.isEmpty(mtokenHead)) {
                edit.putString("tokenHead", mtokenHead);
            }
            if(!TextUtils.isEmpty(headPicFilePath)) {
                edit.putString("headPicPath", headPicFilePath);
            }
            edit.commit();
        }
    }

    /**
     * 获取登录token，用于调用其它接口
     * @return
     */
    public static String getToken() {
        if(TextUtils.isEmpty(mtoken)) {
            SharedPreferences sp = mContext.getSharedPreferences("login",Context.MODE_PRIVATE);
            mtoken = sp.getString("token","");
        }
        return mtoken;
    }

    /**
     * 保存用户登录token
     * @param token
     */
    public void setToken(String token) {
        this.mtoken = token;
        SharedPreferences loginPreference = mContext.getSharedPreferences("login", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = loginPreference.edit();
        edit.putString("token", token);
        edit.commit();
    }

    public String getUsername() {
        if(TextUtils.isEmpty(mUsername)) {
            SharedPreferences sp = mContext.getSharedPreferences("login",Context.MODE_PRIVATE);
            mUsername = sp.getString("username","");
        }
        return mUsername;
    }
      public static void setUsername(String username) {
        mUsername = username;
        if(!TextUtils.isEmpty(mUsername)) {
            SharedPreferences loginPreference = mContext.getSharedPreferences("login", Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = loginPreference.edit();
            edit.putString("username", mtokenHead);
            edit.commit();
        }
    }
    public static String getTokenHead() {
        if(TextUtils.isEmpty(mtokenHead)) {
            SharedPreferences sp = mContext.getSharedPreferences("login",Context.MODE_PRIVATE);
            mtokenHead = sp.getString("tokenHead","");
        }
        return mtokenHead;
    }
    public static void setTokenHead(String tokenHead) {
        mtokenHead = tokenHead;
        if(!TextUtils.isEmpty(mtokenHead)) {
            SharedPreferences loginPreference = mContext.getSharedPreferences("login", Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = loginPreference.edit();
            edit.putString("tokenHead", mtokenHead);
            edit.commit();
        }
    }

    public void setHeadImvUrl(String imvUrl) {
        mHeadImvUrl = imvUrl;
        SharedPreferences loginPreference = mContext.getSharedPreferences("login", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = loginPreference.edit();
        edit.putString("imvUrl", mHeadImvUrl);
        edit.commit();
    }

    public static String getHeadImvUrl() {
        if(TextUtils.isEmpty(mHeadImvUrl)) {
            SharedPreferences sp = mContext.getSharedPreferences("login",Context.MODE_PRIVATE);
            mHeadImvUrl = sp.getString("imvUrl","");
        }
        return mHeadImvUrl;
    }
}
